﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MoneyBook.DialogViews {
    public class DialogBase : ChildWindow {

        public event EventHandler Opened;

        public DialogBase() {
            UpdateUseEscapeKey();
        }

        public void Show(object openArg){
            this.OpenArg = openArg;
            this.Show();
        }

        private bool useEscapeKey_ = true;
        public bool UseEscapeKey {
            get{return this.useEscapeKey_;}
            set {
                if (useEscapeKey_ != value) {
                    useEscapeKey_ = value;
                    UpdateUseEscapeKey();
                }
            }
        }

        private void UpdateUseEscapeKey() {
            this.KeyDown -= DialogBase_KeyDown;
            if (useEscapeKey_) {
                this.KeyDown += DialogBase_KeyDown;
            }
        }

        private void DialogBase_KeyDown(object sender, KeyEventArgs e) {
            if (e.Key != Key.Escape) {
                return;
            }
            e.Handled = true;
            if (this.HasCloseButton) {
                this.Close();
            }
        }

        public object OpenArg { get; private set; }

        public object ResultObject { get; set; }

        protected override void OnOpened() {
            base.OnOpened();
            if (this.Opened != null) {
                this.Opened(this, EventArgs.Empty);
            }
        }
    }
}
